package com.hktf.uilib.db.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hktf.uilib.db.DiaSystemDBHelper;
import com.hktf.uilib.db.bean.RecentSystemBean;

import java.util.ArrayList;
import java.util.List;

public class DiaSystemDao {

	private DiaSystemDBHelper diaSystemDBHelper;

	public DiaSystemDao(Context context) {
		diaSystemDBHelper = new DiaSystemDBHelper(context);
	}

	// 向数据库中增加数据
	public synchronized void insertDiasystemPath(RecentSystemBean bean) {
		if (searchPath(bean.getSysPath(),bean.getSysName())) {
			updatePathInfo(bean);
		} else {
			SQLiteDatabase db = diaSystemDBHelper.getWritableDatabase();
			try {
				String sql = "insert into diasystem_info_tb(name,path,pathID,time,captionpath,isdemo,diasystem_type) values (?,?,?,?,?,?,?)";
				Object[] bindArgs = { bean.getSysName(), bean.getSysPath(),bean.getSysPathID(),
						bean.getSysTime() ,bean.getCaptionPath(),bean.getIsDemo(),bean.getDiasystemType()};
				db.execSQL(sql, bindArgs);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (null != db) {
					db.close();
				}
			}
		}
	}

	/**
	 * 根据path 更新date
	 * 
	 * @param bean
	 */
	public synchronized void updatePathInfo(RecentSystemBean bean) {
		SQLiteDatabase db = diaSystemDBHelper.getWritableDatabase();
		try {
			String sql = "update diasystem_info_tb set time=? where path =? and name=?";
			Object[] bindArgs = { bean.getSysTime(), bean.getSysPath(),bean.getSysName() };
			db.execSQL(sql, bindArgs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != db) {
				db.close();
			}
		}
	}

	/**
	 * 查找该path 是否存在
	 */
	public synchronized boolean searchPath(String path,String name) {
		boolean result = false;
		SQLiteDatabase db = diaSystemDBHelper.getWritableDatabase();
		Cursor cursor = null;
		try {
			cursor = db.query("diasystem_info_tb", null, "path = ? and name = ?",
					new String[] { path,name }, null, null, null);
			if (cursor != null) {
				while (cursor.moveToNext()) {
					result = true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != db) {
				db.close();
			}
			if (null != cursor) {
				cursor.close();
			}
		}
		return result;
	}

	/**
	 * 查询数据库中前20条数据
	 */
	public synchronized List<RecentSystemBean> selectTop20Info() {
		List<RecentSystemBean> datas = new ArrayList<RecentSystemBean>();
		SQLiteDatabase db = diaSystemDBHelper.getWritableDatabase();
		Cursor cursor = null;
		try {
			String sql = "select name,path,pathID,time,captionpath,isdemo,diasystem_type from diasystem_info_tb order by time desc limit 0,20";
			cursor = db.rawQuery(sql, null);
			while (cursor.moveToNext()) {
				RecentSystemBean recentSystemBean = new RecentSystemBean(
						cursor.getString(0), cursor.getString(1),
						cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getInt(5));
				recentSystemBean.setDiasystemType(cursor.getInt(6));
				datas.add(recentSystemBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != db) {
				db.close();
			}
			if (null != cursor) {
				cursor.close();
			}
		}
		return datas;
	}

	/**
	 * 删除一个systeminfo
	 */
	public synchronized void deleteSystemInfo(String path) {
		SQLiteDatabase database = diaSystemDBHelper.getWritableDatabase();
		try {
			database.delete("diasystem_info_tb", "path = ?",
					new String[] { path });
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != database) {
				database.close();
			}
		}
	}

	/**
	 * 清空所有的info信息
	 */
	public synchronized void deleteAllSystemInfo(List<RecentSystemBean> lists) {
		if (lists != null && lists.size() > 0) {
			for (RecentSystemBean item : lists) {
				deleteSystemInfo(item.getSysPath());
			}
		}
	}

}
